#include "common.h"

struct paramter_report_packet parm_report_data;

unsigned char update_ada_parm = 0;
unsigned char update_can_addr = 0;
unsigned char origin_addr     = 0;

void can_app_send(unsigned int messagebox_id, unsigned int cmd_id, unsigned char *data, unsigned char len)
{
    unsigned long long send_data = 0;
    unsigned char     *p         = (unsigned char *)&send_data;
    unsigned int      *p_int     = (unsigned int *)&send_data;

    can->sTxMailBox[messagebox_id].TIR &= ~(1 << 0);
    can->sTxMailBox[messagebox_id].TIR  = (cmd_id << 3) | (1 << 2);
    can->sTxMailBox[messagebox_id].TDTR = len;

    for (unsigned int i = 0; i < len; i++) {
        p[i] = data[i];
    }

    can->sTxMailBox[messagebox_id].TDLR = p_int[0];
    can->sTxMailBox[messagebox_id].TDHR = p_int[1];

    can->sTxMailBox[messagebox_id].TIR |= (1 << 0);
}

void operate_data_report(void)
{
    union can_cmd_id_all cmd_id;
    parm_report_data.PD1      = operate_parm.PD1;
    parm_report_data.T1_Value = operate_parm.T1_Value;
    parm_report_data.T2_Value = operate_parm.T2_Value;
    parm_report_data.T3_Value = operate_parm.T4_Value;
    parm_report_data.EH_Value = operate_parm.EH_Value;
    parm_report_data.ET_Value = operate_parm.ET_Value;
    parm_report_data.ver1     = VER1;
    parm_report_data.ver2     = VER2;
    parm_report_data.sn       = 0;

    cmd_id.all      = 0;
    cmd_id.part.src = adaptor_parm.can_id;
    cmd_id.part.dst = 3;
    cmd_id.part.cmd = 50;

    can_app_send(0, cmd_id.all, (unsigned char *)&parm_report_data, 8);

    cmd_id.part.cmd = 51;
    can_app_send(1, cmd_id.all, ((unsigned char *)&parm_report_data) + 8, 8);

    cmd_id.part.cmd = 52;
    can_app_send(2, cmd_id.all, ((unsigned char *)&parm_report_data) + 16, 8);
}

void USB_LP_CAN1_RX0_IRQHandler(void)
{

    update_can_addr = can->sFIFOMailBox[0].RDLR & 0xff;
    origin_addr     = (can->sFIFOMailBox[0].RIR >> 15) & 0xff;
    can->RF0R       = (1 << 5);
    update_ada_parm = 1;
}

void update_flash_ada_parm(void)
{
    struct response_packet packet;
    union can_cmd_id_all   cmd_response_id;
    if (1 == update_ada_parm) {
        update_ada_parm = 0;
        if (adaptor_parm.can_id == origin_addr) {
            flash_adaptor_parm.part.ada_parm.can_id = update_can_addr;
            flash_adaptor_parm.part.bcp             = 0xa5;
            adaptor_parm.can_id                     = update_can_addr;
            write_flash_adaptor_parm();

            packet.cmd_flag_0        = (300 >> 8) & 0xff;
            packet.cmd_flag_1        = (300) & 0xff;
            packet.can_addr          = adaptor_parm.can_id;
            cmd_response_id.all      = 0;
            cmd_response_id.part.cmd = 0x30e;
            cmd_response_id.part.src = adaptor_parm.can_id;
            cmd_response_id.part.dst = 2;

            can_app_send(0, cmd_response_id.all, (unsigned char *)&packet, 8);
            delay_time(0xfffff);

        } else {
            packet.cmd_flag_0        = (300 >> 8) & 0xff;
            packet.cmd_flag_1        = (300) & 0xff;
            packet.can_addr          = adaptor_parm.can_id;
            cmd_response_id.all      = 0;
            cmd_response_id.part.cmd = 0x30f;
            cmd_response_id.part.src = adaptor_parm.can_id;
            cmd_response_id.part.dst = 2;
            can_app_send(0, cmd_response_id.all, (unsigned char *)&packet, 8);
            delay_time(0xfffff);
        }
    }
}
